﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using System.Data;
using System.Data.Linq;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;

public partial class Evaluate_TaskMonthDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYearList();
            BindMonthList();
            init();
        }
    }

    private void init()
    {
        BindDepartment();
        ListPageContral1.SetFirstPage();
        BindTask();
    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();
    }

    void BindTaskContext(int Taskid)
    {

    }
    private void BindMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            MonthList.Items.Add(li);
        }
        MonthList.SelectedIndex = MonthList.Items.IndexOf(MonthList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }

    private void BindDepartment()
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (departmentId.Substring(0, 4) == "1300")
        {
            RadioButtonList1.Visible = false;
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" Departmentid like '%0" + departmentId.Substring(5, 1) + "0' and Departmentid<>'13000" + departmentId.Substring(5, 1) + "0'");
            DepartmentList.DataBind();
        }
        else
        {
            RadioButtonList1.Visible = true;
            string key = RadioButtonList1.SelectedValue;
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentId like '" + departmentId.Substring(0, 4) + key + "%'");
            DepartmentList.DataTextField = "ShortName";
            DepartmentList.DataBind();
        }
    }
    protected void lbn_Click(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindTask();
    }
    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        GV.PageIndex = PageIndex;
        BindTask();
    }
    void BindTask()
    {

        string sql = "";
        string CreateDepartment = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        if (Request["SM"] != null)
        {
            string SM = Request["SM"];
            string EM = Request["EM"];
            string Year = Request["Year"];
            string DepartmentID = Request["DepartmentID"];
            if (CreateDepartment.Substring(0, 4) != "1300")
            {
                sql = "select * from Tasks where CreateDepartment like '" + CreateDepartment.Substring(0, 6) + "%' and YEAR(EndDate) =" + Year + " AND MONTH(EndDate)>=" + SM + " AND MONTH(EndDate)<=" + EM + "  and Taskid in (select TaskId from TaskUsers where userid='" + DepartmentID + "' ) order by TaskId";

            }
            else
            {
                sql = "select * from Tasks where substring(CreateDepartment,5,1)='0' and substring(CreateDepartment,6,1)='" + DepartmentList.SelectedValue.Substring(5, 1) + "' and YEAR(EndDate) =" + Year + " AND MONTH(EndDate)>=" + SM + " AND MONTH(EndDate)<=" + EM + "  and Taskid in (select TaskId from TaskUsers where userid='" + DepartmentID + "' ) order by TaskId";
                tm.Visible = false;
                divPromt.Visible = true;
                StandardInfo standard = Standard.GetStandard();
                decimal TotalScore = standard.TaskPer;
                lblPerent.Text = Convert.ToString(TotalScore * 100);
                lblTotle.Text = standard.TotalScore.ToString("0");
                lblGS.Text = Request["GS"];
            }
        }
        else
        {
            tm.Visible = true;
            string departmentid = DepartmentList.SelectedValue;
            if (CreateDepartment.Substring(0, 4) != "1300")
            {
                sql = "select * from Tasks where CreateDepartment like '" + CreateDepartment.Substring(0, 6) + "%' and  YEAR(EndDate) =" + YearList.SelectedValue + " AND MONTH(EndDate)=" + MonthList.SelectedValue + " and Taskid in (select TaskId from TaskUsers where userid='" + departmentid + "' ) order by TaskId";
            }
            else
            {
                sql = "select * from Tasks where substring(CreateDepartment,5,1)='0' and substring(CreateDepartment,6,1)='" + DepartmentList.SelectedValue.Substring(5, 1) + "' and YEAR(EndDate) =" + YearList.SelectedValue + " AND MONTH(EndDate)=" + MonthList.SelectedValue + " and Taskid in (select TaskId from TaskUsers where userid='" + departmentid + "' ) order by TaskId";
            }

            divPromt.Visible = false;
        }


        DataSet ds = SplitPage.ExecuteSQL(sql);
        ListPageContral1.ItemCount = ds.Tables[0].Rows.Count;
        GV.DataSource = ds.Tables[0];
        GV.DataBind();
    }

    protected void gvChemical_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string departmentid = DepartmentList.SelectedValue;
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(GV.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            DataRowView dv = e.Row.DataItem as DataRowView;
            LinkButton lbtnView = e.Row.FindControl("lbtnView") as LinkButton;
            lbtnView.Text = dv["Title"].ToString();
            Label lblCreateInfo = e.Row.FindControl("lblCreateInfo") as Label;
            lblCreateInfo.Text = "[" + dv["CreateUser"] + ":" + Convert.ToDateTime(dv["CreateDate"]).ToShortDateString() + "]";

            TaskLogInfo log = TaskLog.GetTaskLog(Convert.ToInt32(dv["TaskId"]), departmentid);
            if (log != null)
            {
                Label Circs = e.Row.FindControl("Circs") as Label;
                Circs.Text = log.Progress == null ? "" : log.Progress.ToString();
            }
            TaskUserInfo user = TaskUser.GetTaskUser(Convert.ToInt32(dv["TaskId"]), departmentid);
            if (user.Evaluated)
            {
                CheckBox cb = e.Row.FindControl("cb") as CheckBox;
                cb.Checked = true;
                Label Sorce = e.Row.FindControl("Sorce") as Label;
                Sorce.Text = user.Score.ToString();
            }

        }
    }
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindTask();
    }

    private void BindData(int RootId)
    {
        string departmentid = DepartmentList.SelectedValue;
        TaskLogInfo log;
        string[] CnNums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        StringBuilder sb = new StringBuilder();
        List<TaskInfo> tasks = Task.GetTasks(RootId);
        sb.Append("<div class=title>" + tasks[0].Title + "</div>");
        sb.Append("<div class=date><strong>" + Department.GetDepartment(departmentid).ShortName + "</strong> ");
        sb.Append("[" + tasks[0].StartDate.ToShortDateString() + "—" + tasks[0].EndDate.ToShortDateString() + "] ");
        TaskUserInfo user = TaskUser.GetTaskUser(RootId, departmentid);
        sb.Append("实得分：" + user.Score.ToString() + " ");
        if (user.Evaluated)
        {
            sb.Append("<img src=../images/icon_evaluated.gif align=absMiddle>");
        }
        else
        {
            sb.Append("<img src=../images/icon_unevaluated.gif align=absMiddle>");
        }
        sb.Append("</div>");
        if (tasks[0].Comment != "")
        {
            sb.Append("<div class=comment>" + UserFun.TextToHtml(tasks[0].Comment) + "</div>");
        }
        //显示附件
        if (tasks[0].Files.Count > 0)
        {
            foreach (TaskFileInfo file in tasks[0].Files)
            {
                sb.Append("<div class=file><img src=../images/attachment.gif alt=> <a href=../Upload" + file.FileName + " target=_blank>" + file.FileTitle + "</a></div>");
            }
        }
        log = TaskLog.GetTaskLog(tasks[0].TaskId, departmentid);
        if (log != null)
        {
            sb.Append("<div class=report>" + UserFun.TextToHtml(log.Comment) + "<br>");
            //显示附件
            if (log.Files.Count > 0)
            {
                foreach (TaskLogFileInfo file in log.Files)
                {
                    sb.Append("<div class=file><img src=../images/attachment.gif alt=><a href=../Upload" + file.FileName + " target=_blank>" + file.FileTitle + "</a></div>");
                }
            }
            sb.Append("<div class=remark>完成进度：" + log.Progress.ToString() + "%<br>汇报单位：" + log.UpdateDepartment + "<br>汇 报 人：" + log.UpdateUser + "<br>汇报时间：" + log.CreateDate.ToString() + "</div></div>");
        }
        int j = 0;
        int k = 1;
        for (int i = 1; i < tasks.Count; i++)
        {
            TaskInfo task = tasks[i];
            if (task.Depth == 1)
            {
                sb.Append("<div class=subtitle>");
                sb.Append("第" + CnNums[j] + "阶段、");
                j++;
                k = 1;
            }
            else
            {
                sb.Append("<div class=thtitle>");
                sb.Append(k.ToString() + "、");
                k++;
            }
            sb.Append(task.Title + "[" + task.StartDate.ToString("MM-dd") + "—" + task.EndDate.ToString("MM-dd") + "]");
            sb.Append("</div>");
            if (tasks[i].Comment != "")
            {
                sb.Append("<div class=comment>" + UserFun.TextToHtml(task.Comment) + "</div>");
            }
            //显示附件
            if (task.Files.Count > 0)
            {
                foreach (TaskFileInfo file in task.Files)
                {
                    sb.Append("<div class=file><img src=../images/attachment.gif alt=> <a href=../Upload" + file.FileName + " target=_blank>" + file.FileTitle + "</a></div>");
                }
            }
            log = TaskLog.GetTaskLog(task.TaskId, departmentid);
            if (log != null)
            {
                sb.Append("<div class=report>" + UserFun.TextToHtml(log.Comment) + "<br>");
                //显示附件
                if (log.Files.Count > 0)
                {
                    foreach (TaskLogFileInfo file in log.Files)
                    {
                        sb.Append("<div class=file><img src=../images/attachment.gif alt=><a href=../Upload" + file.FileName + " target=_blank>" + file.FileTitle + "</a></div>");
                    }
                }
                sb.Append("<div class=remark>完成进度：" + log.Progress.ToString() + "%<br>汇报单位：" + log.UpdateDepartment + "<br>汇 报 人：" + log.UpdateUser + "<br>汇报时间：" + log.CreateDate.ToString() + "</div></div>");
            }
        }
        sb.Append("<br /><div class=comment>");
        sb.Append("评语：" + (user.Description == "" ? "无" : user.Description));
        sb.Append("</div>");

        Body.Text = sb.ToString();
    }
    public string GetUserName(string userId)
    {
        return Department.GetDepartment(userId).ShortName;
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "View")
        {
            BindData(Convert.ToInt32(e.CommandArgument));
        }
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        init();
    }
}
